<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>商品服务</span>
      </div>
    </template>
    <el-row :gutter="24" class="elRow">
      <div>
        <!-- 商品类型选择 -->
        <el-select
          style="margin-right: 7px; width: 200px"
          v-model="pageData.bigTypeId"
          placeholder="商品类型"
          clearable
          @change="bigChange"
        >
          <el-option
            v-for="item in bigTypeList"
            :key="item.val"
            :label="item.name"
            :value="item.val"
          />
        </el-select>

        <!-- 商品状态选择 -->
        <el-select
          style="width: 200px"
          v-model="pageData.status"
          placeholder="商品状态"
          clearable
          @change="selectChange"
        >
          <el-option
            v-for="item in statusList"
            :key="item.val"
            :label="item.name"
            :value="item.val"
          />
        </el-select>
      </div>
      <div>
        <!-- 新建服务按钮 -->
        <el-button
          type="primary"
          class="btn"
          @click="createService"
          v-hasPermi="['operation:create:add']"
          >新建服务</el-button
        >
      </div>
    </el-row>

    <!-- 商品列表表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      class="margintop"
      stripe
      v-loading="tableLoading"
      :header-cell-style="{ 'border-bottom': 'none' }"
      :header-row-style="{ height: '60px' }"
      :cell-style="{ height: '60px', 'border-bottom': 'none' }"
    >
      <!-- 排序 -->
      <el-table-column prop="sort" label="排序">
        <template #default="scope">
          <el-popover
            :popper-style="{ width: '170px' }"
            placement="top-start"
            :visible="visible"
          >
            <div class="popover">
              <el-input-number v-model="scope.row.sort" :min="0" />
              <p class="poverBtn" @click="visible = false">确定</p>
            </div>
            <template #reference>
              <p style="cursor: pointer" @click="sort(scope.row)">
                {{ scope.row.sort }}
              </p>
            </template>
          </el-popover>
        </template>
      </el-table-column>

      <!-- 商品名称 -->
      <el-table-column prop="name" label="商品名称" />

      <!-- 商品类型 -->
      <el-table-column prop="bigTypeName" label="商品类型" align="center" />

      <!-- 销售价 -->
      <el-table-column prop="basePrice" label="销售价" align="center" />

      <!-- 折扣价 -->
      <el-table-column prop="realPrice" label="折扣价" align="center" />

      <!-- 销售量 -->
      <el-table-column prop="sellNum" label="销售量" align="center" />

      <!-- 商品状态 -->
      <el-table-column prop="status" label="商品状态" align="center">
        <template #default="scope">
          <span>{{ scope.row.status === 0 ? '下架' : '上架' }}</span>
        </template>
      </el-table-column>

      <!-- 操作 -->
      <el-table-column prop="type" label="操作" align="center">
        <template #default="scope">
          <!-- 编辑 -->
          <el-tooltip
            class="box-item"
            effect="dark"
            content="编辑"
            placement="top"
          >
            <el-icon
              size="20"
              style="cursor: pointer"
              @click="editChange(scope.row)"
              v-hasPermi="['operation:goodservce:edit']"
            >
              <EditPen />
            </el-icon>
          </el-tooltip>

          <!-- 上架/下架 -->
          <el-tooltip
            class="box-item"
            effect="dark"
            :content="scope.row.status === 0 ? '上架' : '下架'"
            placement="top"
          >
            <el-icon
              size="20"
              style="margin: 0 10px"
              @click="downChange(scope.row)"
              v-hasPermi="['operation:goodservce:edit']"
            >
              <Top class="curr" v-if="scope.row.status == 0" />
              <Bottom class="curr" v-else />
            </el-icon>
          </el-tooltip>

          <!-- 删除 -->
          <el-tooltip
            class="box-item"
            effect="dark"
            content="删除"
            placement="top"
          >
            <el-icon
              size="20"
              v-hasPermi="['operation:goodservce:edit']"
              style="cursor: pointer; margin-left: 10px"
            >
              <Delete @click="deleteChange(scope.row)" />
            </el-icon>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="right" style="margin-top: 20px">
      <el-config-provider :locale="zhCn">
        <el-pagination
          v-model:current-page="pageData.page"
          v-model:page-size="pageData.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-config-provider>
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import {
  allServicesList,
  servicesListFilter,
  servicesDel,
  servicesDown,
} from '@/api/service/service.js';

// 响应式数据
const tableLoading = ref(false);
const visible = ref(false);
const router = useRouter();
const tableData = ref([]); // 商品列表
const num = ref(0);
const id = ref(null);
const total = ref(0);
const bigTypeList = ref([]); // 商品类型列表
const statusList = ref([]); // 商品状态列表

// 分页参数
const pageData = ref({
  page: 1,
  pageSize: 10,
  status: '',
  bigTypeId: '',
});

// 生命周期钩子
onMounted(() => {
  getGoodsService();
  servicesListChange();
});

// 获取商品类型和状态
function getGoodsService() {
  servicesListFilter().then((res) => {
    bigTypeList.value = res.data[0].data;
    statusList.value = res.data[1].data;
  });
}

// 获取商品列表
function servicesListChange() {
  tableLoading.value = true;
  allServicesList({ ...pageData.value })
    .then((res) => {
      if (res.code === 20000) {
        tableData.value = res.data.data;
        total.value = res.data.total;
      }
    })
    .finally(() => {
      tableLoading.value = false;
    });
}

// 商品类型选择变化
function bigChange() {
  pageData.value.page = 1; // 重置页码
  servicesListChange();
}

// 商品状态选择变化
function selectChange() {
  pageData.value.page = 1; // 重置页码
  servicesListChange();
}

// 分页大小变化
function handleSizeChange(size) {
  pageData.value.pageSize = size;
  servicesListChange();
}

// 当前页码变化
function handleCurrentChange(page) {
  pageData.value.page = page;
  servicesListChange();
}

// 上架/下架
function downChange(val) {
  const status = val.status === 1 ? 0 : 1;
  servicesDown({ id: val.id, status }).then((res) => {
    ElMessage.success(res.msg);
    servicesListChange();
  });
}

// 删除商品
function deleteChange(val) {
  ElMessageBox.confirm('是否删除当前列表数据信息', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      servicesDel({ id: val.id }).then(() => {
        ElMessage.success('删除成功');
        servicesListChange();
      });
    })
    .catch(() => {
      ElMessage.info('取消删除');
    });
}

// 排序
function sort(val) {
  num.value = val.sort;
  id.value = val.id;
}

// 新建服务
function createService() {
  router.push('/operation/create');
}

// 编辑商品
function editChange(val) {
  router.push({
    path: '/operation/goodServce',
    query: {
      id: val.id,
    },
  });
}
</script>

<style lang="scss" scoped>
:deep(.el-form--inline .el-form-item) {
  margin-right: 7px;
}

:deep(.el-form-item--default) {
  margin-bottom: 4px;
}

.poverBtn {
  cursor: pointer;
  margin-top: 10px;
  color: #165dff;
  font-size: 14px;
}

.elRow {
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
}

.box-card {
  margin: 20px;

  .card-header {
    font-size: 20px;
    font-weight: 600;
    color: #1d2129;
    padding: 10px;

    .headerSize {
      color: #4e5969;
      font-size: 14px;
      margin-left: 12px;
      font-weight: 400;
      height: 20px;
      line-height: 21px;
    }
  }
}
</style>
